import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InfraJobLogApi } from '#/api/infra/job-log';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDateTime } from '@vben/utils';

import dayjs from 'dayjs';

import { DictTag } from '#/components/dict-tag';

import { $t } from '#/locales';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'handlerName',
      label: $t('views.infra.job.handlerName'),
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.infra.job.handlerNamePlaceholder'),
      },
    },
    {
      fieldName: 'beginTime',
      label: $t('views.infra.job.logger.beginTime'),
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.infra.job.logger.beginTimePlaceholder'),
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        showTime: {
          format: 'HH:mm:ss',
          defaultValue: dayjs('00:00:00', 'HH:mm:ss'),
        },
      },
    },
    {
      fieldName: 'endTime',
      label: $t('views.infra.job.logger.endTime'),
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        placeholder: $t('views.infra.job.logger.endTimePlaceholder'),
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        showTime: {
          format: 'HH:mm:ss',
          defaultValue: dayjs('23:59:59', 'HH:mm:ss'),
        },
      },
    },
    {
      fieldName: 'status',
      label: $t('views.infra.job.status'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_JOB_LOG_STATUS, 'number'),
        allowClear: true,
        placeholder: $t('views.infra.job.statusPlaceholder'),
      },
    },
  ];
}

/** 表格列配置 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 80,
    },
    {
      field: 'jobId',
      title: $t('views.infra.job.logger.jobId'),
      minWidth: 80,
    },
    {
      field: 'handlerName',
      title: $t('views.infra.job.handlerName'),
      minWidth: 180,
    },
    {
      field: 'handlerParam',
      title: $t('views.infra.job.handlerParam'),
      minWidth: 140,
    },
    {
      field: 'executeIndex',
      title: $t('views.infra.job.logger.executeIndex'),
      minWidth: 100,
    },
    {
      field: 'beginTime',
      title: $t('views.infra.job.logger.beginTime'),
      minWidth: 280,
      formatter: ({ row }) => {
        return `${formatDateTime(row.beginTime)} ~ ${formatDateTime(row.endTime)}`;
      },
    },
    {
      field: 'duration',
      title: $t('views.infra.job.logger.duration'),
      minWidth: 120,
      formatter: ({ row }) => {
        return `${row.duration} 毫秒`;
      },
    },
    {
      field: 'status',
      title: $t('views.infra.job.status'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_JOB_LOG_STATUS },
      },
    },
    {
      title: $t('common.action'),
      width: 80,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 详情页的字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      field: 'id',
      label: $t('views.common.id'),
    },
    {
      field: 'jobId',
      label: $t('views.infra.job.logger.jobId'),
    },
    {
      field: 'handlerName',
      label: $t('views.infra.job.handlerName'),
    },
    {
      field: 'handlerParam',
      label: $t('views.infra.job.handlerParam'),
    },
    {
      field: 'executeIndex',
      label: $t('views.infra.job.logger.executeIndex'),
    },
    {
      field: 'beginTime',
      label: $t('views.infra.job.logger.beginTime'),
      content: (data: InfraJobLogApi.JobLog) => {
        if (data?.beginTime && data?.endTime) {
          return `${formatDateTime(data.beginTime)} ~ ${formatDateTime(data.endTime)}`;
        }
        return '';
      },
    },
    {
      field: 'duration',
      label: $t('views.infra.job.logger.duration'),
      content: (data: InfraJobLogApi.JobLog) => {
        return data?.duration ? `${data.duration} 毫秒` : '';
      },
    },
    {
      field: 'status',
      label: $t('views.infra.job.status'),
      content: (data: InfraJobLogApi.JobLog) => {
        return h(DictTag, {
          type: DICT_TYPE.INFRA_JOB_LOG_STATUS,
          value: data?.status,
        });
      },
    },
    {
      field: 'result',
      label: $t('views.infra.job.logger.result'),
    },
  ];
}
